package org.zfes.snowier.cloud.zbss.metting.biz.service.impl;

import java.util.Map;
import java.util.Optional;
import org.zfes.snowier.core.data.DataSet;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.zfes.snowier.core.util.ZBeanUtil;
import org.zfes.snowier.core.util.ZDateUtil;
import org.zfes.snowier.core.util.ZAlert;
import org.zfes.snowier.core.util.ZAssert;
import org.zfes.snowier.common.dao.params.ParamMap;
import org.zfes.snowier.cloud.zbss.metting.biz.service.ITenantMemberService;
import org.zfes.snowier.cloud.zbss.metting.biz.dao.TenantMemberMapper;
import org.zfes.snowier.cloud.zbss.metting.biz.model.TenantMember;
import java.util.stream.Stream;
@Service
public class TenantMemberServiceImpl implements ITenantMemberService{
 	@Autowired
	private TenantMemberMapper tenantMemberMapper;
	
	@Transactional(rollbackFor=Exception.class)
 	@Override
	public void createTenantMember(TenantMember tenantMember) {
		ZBeanUtil.setCreateInfo(tenantMember);
		ZBeanUtil.validateBean(tenantMember);
		
		Object id3=tenantMemberMapper.selectIdByAccount(tenantMember.getAccount());
		if(id3!=null){
			ZAlert.Error("账号已经存在");
		}
		
		Object id7=tenantMemberMapper.selectIdByPhone(tenantMember.getPhone());
		if(id7!=null){
			ZAlert.Error("手机号已经存在");
		}
		
		Object id8=tenantMemberMapper.selectIdByEmail(tenantMember.getEmail());
		if(id8!=null){
			ZAlert.Error("邮箱已经存在");
		}
		tenantMemberMapper.insertSelective(tenantMember);
	}
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public Optional<TenantMember> loadTenantMemberById(Long id) {
		if(id!=null){
			return Optional.ofNullable(tenantMemberMapper.selectByPrimaryKey(id));
		}
		return Optional.empty();
		
	}
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void updateTenantMember(TenantMember tenantMember) {
		ZBeanUtil.setUpdatedInfo(tenantMember);
		ZBeanUtil.validateBean(tenantMember);
		Object id3=tenantMemberMapper.selectIdByAccount(tenantMember.getAccount());
		if(id3!=null&&!id3.equals(tenantMember.getId())){
			ZAlert.Error("账号已经存在");
		}
		Object id7=tenantMemberMapper.selectIdByPhone(tenantMember.getPhone());
		if(id7!=null&&!id7.equals(tenantMember.getId())){
			ZAlert.Error("手机号已经存在");
		}
		Object id8=tenantMemberMapper.selectIdByEmail(tenantMember.getEmail());
		if(id8!=null&&!id8.equals(tenantMember.getId())){
			ZAlert.Error("邮箱已经存在");
		}
		tenantMemberMapper.updateByPrimaryKeySelective(tenantMember);
	}
	
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void deleteTenantMember(Long id) {
		ZAssert.notNull(id, "请选择要操作的数据");
		tenantMemberMapper.deleteByPrimaryKey(id);
	}
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void setTenantMemberStatus(Long id, Byte status) {
		ZAssert.notNull(id, "请选择要操作的数据");
		ZAssert.notNull(status, "状态参数错误");
		if(!Stream.of(new Byte[] {-1,0,1}).anyMatch(val->val.equals(status))) {
			ZAlert.serviceLogicalError("状态参数错误");
		}
		tenantMemberMapper.updateStatus( id,  status) ;
	}
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public DataSet loadTenantMemberDataSet(Map<String, Object> params) {
		ParamMap pm=ParamMap.filterParam(params);
		DataSet ds=DataSet.newDS2(tenantMemberMapper.selectListCount(pm), tenantMemberMapper.selectMapListPage(pm));
		return ds;
	}
	
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public Optional<TenantMember> loadByAccount(java.lang.String account){
		if(account!=null){
			return Optional.ofNullable(tenantMemberMapper.selectByAccount(account));
		}
		return Optional.empty();
	}
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public Optional<TenantMember> loadByPhone(java.lang.String phone){
		if(phone!=null){
			return Optional.ofNullable(tenantMemberMapper.selectByPhone(phone));
		}
		return Optional.empty();
	}
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public Optional<TenantMember> loadByEmail(java.lang.String email){
		if(email!=null){
			return Optional.ofNullable(tenantMemberMapper.selectByEmail(email));
		}
		return Optional.empty();
	}
}